﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutomationSRAS.Config.Automation;
using NetworkSimulator.SimulatorComponents;
using NetworkSimulator;
using System.Diagnostics;
using AutomationSRAS.config.simulator;

namespace AutomationSRAS
{
    public class Program
    {
        static void Main(string[] args)
        {
            AutomationConfigurator cfg = AutomationConfigurator.getInstance();
            SimulationConfigurator scfg = SimulationConfigurator.Deserialize(cfg.Simulator.WorkingDir + cfg.Simulator.ConfigFile);

            foreach (TopologyCfg topology in cfg.topologyList.topologies)
            {
                scfg.TopologyFilePath = cfg.Simulator.WorkingDir + topology.filepath;
                scfg.IEListFilePath = cfg.Simulator.WorkingDir + topology.IEListFilePath;
                foreach (RequestCfg request in topology.requestList.requests)
                {
                    scfg.RequestFilePath = topology.requestDirectory + request.filename;
                    scfg.StatisticsFilepath = topology.resultDirectory + request.filename;
                    foreach (AutomationSRAS.Config.Automation.AlgorithmCfg algorithm in cfg.algorithmList.algorithms)
                    {

                        scfg.UnicastRoutingAlgorithms.setSelectedAlgorithm(algorithm.name);
                        SimulationConfigurator.Serialize(cfg.Simulator.ConfigFile, scfg);

                        Console.Write("Topology[" + topology.name + "] \nRequest[" + request.filename + "] \nAlgorithm[" + algorithm.name + "] \nProcessing... " );
                        // Run simulator
                        Process simulator = new Process();
                        simulator.StartInfo.FileName = cfg.Simulator.WorkingDir + cfg.Simulator.ExecutorName;
                        simulator.EnableRaisingEvents = true;

                        if (!cfg.Simulator.ShowUI)
                        {
                            simulator.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        }

                        simulator.Start();
                        simulator.WaitForExit();
                        Console.WriteLine("[DONE]\n");
                    }
                }
            }
            Console.WriteLine("Automation Job DONE.");
            Console.Write("Press any key to exit...");
            Console.ReadKey();
        }

    }
}
